package cloud.xlh.my_system.beanpostprocessor;

import cloud.xlh.constant.MonitorKeyConstant;
import cloud.xlh.constant.MonitorLabelConstant;
import cloud.xlh.domin.DO.MonitorCounterDO;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/2/3 14:38
 * @ Description: 中间件细粒度解密程序
 */
@Component
@Slf4j
public class MeterRegistryBeanPostProcessor implements BeanPostProcessor {

    @Autowired
    private MonitorCounterDO monitorCounterDO;

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MeterRegistry) {
            log.info("MeterRegistryBeanPostProcessor postProcessAfterInitialization run...");
            MeterRegistry meterRegistryBean = (MeterRegistry) bean;
            register(meterRegistryBean);
        }
        return bean;
    }

    public boolean register(MeterRegistry meterRegistryBean) {

        Counter requestTotalTimes = monitorCounterDO.getRequestTotalTimes();
        requestTotalTimes = meterRegistryBean.counter(MonitorKeyConstant.REQUEST_TOTAL_TIMES
                , MonitorLabelConstant.APPLICATION , "my-system"
                ,   MonitorLabelConstant.SERVICE_NAME , "1d01测试服务"
                , MonitorLabelConstant.PATH , "/1d01"
                , MonitorLabelConstant.CHANNEL , "esb");
        monitorCounterDO.setRequestTotalTimes(requestTotalTimes);
/*        requestTotalTimes = meterRegistryBean.counter(MonitorKeyConstant.REQUEST_TOTAL_TIMES
                , MonitorLabelConstant.APPLICATION , "my-system"
                ,   MonitorLabelConstant.SERVICE_NAME , "1d02测试服务"
                , MonitorLabelConstant.PATH , "/1d02"
                , MonitorLabelConstant.CHANNEL , "esb");*/
        Counter requestSuccessTimes = monitorCounterDO.getRequestSuccessTimes();
        requestSuccessTimes = meterRegistryBean.counter(MonitorKeyConstant.REQUEST_SUCCESS_TIMES
                , MonitorLabelConstant.APPLICATION , "my-system"
                ,   MonitorLabelConstant.SERVICE_NAME , "1d01测试服务"
                , MonitorLabelConstant.PATH , "/1d01"
                , MonitorLabelConstant.CHANNEL , "esb");
        return true;
    }

    public MonitorCounterDO getMonitorCounterDO() {
        return monitorCounterDO;
    }
}
